﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using MiniWeb.Core.Dtos;
using MiniWeb.Core.Entities;
using MiniWeb.Core.Enums;
using MiniWeb.Task;
using MiniWeb.Web.Controllers.Web.usercenter.ViewModels;
using MiniWeb.Web.Controllers.Web.web.ViewModels;
using WFramework;
using WFramework.Common;
using WFramework.Extensions;
using WFramework.Mappers;
using WFramework.MvcPager;
using WFramework.Utility;

namespace MiniWeb.Web.Controllers.Web.UserCenter
{
    public class UserCenterController : UserBaseController
    {
        public readonly AccountsInfoTask _AccountsInfoTask = new AccountsInfoTask();
        public readonly OrderAddressTask _OrderAddressTask = new OrderAddressTask();
        public readonly OrdersTask _OrdersTask = new OrdersTask();
        public readonly OrdersDetailTask _OrdersDetailTask = new OrdersDetailTask();
        public ActionResult Index()
        {
            var model = _AccountsInfoTask.GetByAccount(WebFormsAuthService.GetCurrentAccount());

            return View(model);
        }


        public ActionResult Orders(int pageindex = 1, int pagesize = 10)
        {
            var account = WebFormsAuthService.GetCurrentAccount();
            var pagedList = _OrdersTask.GetPagedList(account, null, null, null, null, null, pageindex, pagesize);
            ViewData["orderstatelist"] = OrderStateEnum.已支付.ToSelectListForAll();
            ViewData["paytypelist"] = PayTypeEnum.后台处理.ToSelectListForAll();
            var model = new PagedList<Orders>(pagedList.ToList(), pageindex, pagesize, pagedList.TotalItemCount);
            return View(model);

        }

        public ActionResult OrderShow(int id)
        {
            return View();
        }
        
        public ActionResult orderdetail(string code)
        {
            var ordermodel = _OrdersTask.GetByCode(code);
            var orderlistmodel = _OrdersDetailTask.GetListByCode(code);
            if (orderlistmodel == null)
            {
                return new EmptyResult();
            }
            ViewData["orderlistmodel"] = orderlistmodel;
            var orderaddressmodel = _OrderAddressTask.GetByAccount(ordermodel.Account);
            if (orderlistmodel == null)
            {
                return AlertMsg("请填写收获地址信息", @Url.Action("address", "usercenter"));
            }
            ViewData["orderaddressmodel"] = orderaddressmodel;
            return View(ordermodel);
        }


        //个人信息
        public ActionResult Info()
        {
            return View();
        }

        public ActionResult ChangePwd()
        {
            return View();
        }
        [HttpPost]
        public ActionResult ChangePwd(string pwd, string cpwd, string cpw1)
        {
            if (string.IsNullOrEmpty(cpwd) || string.IsNullOrEmpty(pwd))
            {
                return AlertMsg("密码不能为空！", Request.UrlReferrer.ToString());
            }
            if (cpwd != cpw1)
            {
                return AlertMsg("密码不一致！", Request.UrlReferrer.ToString());
            }
            var model = _AccountsInfoTask.GetByAccount(WebFormsAuthService.GetCurrentAccount());
            if (model == null)
            {
                return new EmptyResult();
            }
            if (model.Password != CryptTools.HashPassword(pwd))
                return AlertMsg("账号或密码不正确", Request.UrlReferrer.PathAndQuery);
            model.Password = CryptTools.HashPassword(cpwd);
            _AccountsInfoTask.Update(model);
            return AlertMsg("修改成功！", "/usercenter/changepwd");

        }

        public ActionResult Address(int? id)
        {


            var savemodel = new WebSaveOrderAddress();
            if (id != null)
            {
                var model = _OrderAddressTask.GetById((int)id);
                if (model == null)
                {
                    return new EmptyResult();
                }
                savemodel = EntityMapper.Map<OrderAddress, WebSaveOrderAddress>(model);

            }
            ViewData["myaddresslist"] = _OrderAddressTask.GetByAccount(WebFormsAuthService.GetCurrentAccount());
            return View(savemodel);

        }

        [HttpPost]
        public ActionResult SaveAddress(WebSaveOrderAddress savemodel)
        {

            if (ModelState.IsValid)
            {

                if (savemodel.ID == null)
                {
                    var model = new OrderAddress
                                    {
                                        Account = WebFormsAuthService.GetCurrentAccount(),
                                        RealName = savemodel.RealName,
                                        Phone = savemodel.Phone,
                                        Address = savemodel.Address,
                                        Sheng = savemodel.Sheng,
                                        City = savemodel.City,
                                        IsDefault = savemodel.IsDefault,
                                        CreateDate = DateTime.Now,
                                        Qu = savemodel.Qu,
                                        StmpCode = savemodel.StmpCode,
                                        Tel = savemodel.Tel,
                                    };
                    if (savemodel.IsDefault)
                    {
                        _OrderAddressTask.SetUnDefault(LoginAccount);
                    }
                    _OrderAddressTask.Add(model);



                }
                else
                {
                    var model = _OrderAddressTask.GetById((int)savemodel.ID);

                    if (model == null)
                        return Json(new { msg = 0, msgbox = "参数错误" });

                    model.RealName = savemodel.RealName;
                    model.Phone = savemodel.Phone;
                    model.Address = savemodel.Address;
                    model.Sheng = savemodel.Sheng;
                    model.City = savemodel.City;
                    model.IsDefault = savemodel.IsDefault;
                    if (model.IsDefault)
                    {
                        _OrderAddressTask.SetUnDefault(LoginAccount);
                    }
                    model.StmpCode = savemodel.StmpCode;
                    model.Qu = savemodel.Qu;
                    model.Tel = savemodel.Tel;
                    _OrderAddressTask.Update(model);

                }

                return Json(new { msg = 1, msgbox = "保存成功" });

            }


            return Json(new { msg = 0, msgbox = "保存失败" });


        }


        public ActionResult SetDefaultAddress(int id)
        {
            var model = _OrderAddressTask.GetById(id);
            if (model.Account == LoginAccount)
            {
                _OrderAddressTask.SetUnDefault(model.Account);
                model.IsDefault = true;
                _OrderAddressTask.Update(model);


            }
            return RedirectToAction("Address");
        }

        [HttpPost]
        public ActionResult DeleteAddress(int id)
        {
            _OrderAddressTask.Delete(id);
            return Json(new { msg = 1, msgbox = "删除成功!" });
        }

    }
}
